/**
 * 
 */
package com.thor.palmmall.rest.product;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.entity.BeanOperateInfo;
import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.query.QueryResult;
import com.thor.commons.util.Assert;
import com.thor.palmmall.rest.common.AUserGroupLoadParam;
import com.thor.palmmall.rest.common.SUserGroupLoadParamConverter;
import com.thor.palmmall.rest.product.converter.AProductChangeConverter;
import com.thor.palmmall.rest.product.converter.SProductChange4CreateConverter;
import com.thor.palmmall.rest.product.converter.SProductChangeConverter;
import com.thor.palmmall.rest.product.converter.SProductChangeQueryParamConverter;
import com.thor.palmmall.spi.common.SOperateInfoConverter;
import com.thor.palmmall.spi.common.SQueryResult;
import com.thor.palmmall.spi.common.SQueryResultConverter;
import com.thor.palmmall.spi.common.SResponse;
import com.thor.palmmall.spi.product.SProductChange;
import com.thor.palmmall.spi.product.SProductChangeService;

/**
 * @author NuoSa
 *
 * @since 1.0
 */
public class AProductChangeServiceImpl implements AProductChangeService {

  @Override
  public String create(AProductChange productChange, BeanOperateInfo operateInfo)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(productChange, "productChange");
    Assert.assertArgumentNotNull(operateInfo, "operateInfo");
    Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");

    SResponse<String> resp = sProductChangeService.create(
        SProductChange4CreateConverter.getInstance().convert(productChange),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess())
      return resp.getBody();
    else
      throw new ThorServiceException("无法录入商品变更单：" + resp.getMessage());
  }

  @Override
  public AProductChange get(String id, AUserGroupLoadParam param) throws ThorServiceException {
    SResponse<SProductChange> resp = sProductChangeService.get(id,
        SUserGroupLoadParamConverter.getInstance().convert(param));
    if (resp.isSuccess())
      return AProductChangeConverter.getInstance().convert(resp.getBody());
    else
      throw new ThorServiceException("无法找到指定ID的商品变更单：" + resp.getMessage());
  }

  @Override
  public String save(AProductChange productChange, BeanOperateInfo operateInfo)
      throws ThorServiceException {
    Assert.assertArgumentNotNull(productChange, "productChange");
    Assert.assertArgumentNotNull(operateInfo, "operateInfo");
    Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
    Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");
    SResponse<String> resp = sProductChangeService.save(
        SProductChangeConverter.getInstance().convert(productChange),
        SOperateInfoConverter.getInstance().convert(operateInfo));
    if (resp.isSuccess())
      return resp.getBody();
    else
      throw new ThorServiceException("无法保存商品变更单：" + resp.getMessage());

  }

  @Override
  public QueryResult<AProductChange> query(AProductChangeQueryParam param)
      throws ThorServiceException {

    SResponse<SQueryResult<SProductChange>> resp = sProductChangeService
        .query(SProductChangeQueryParamConverter.getInstance().convert(param));
    if (resp.isSuccess())
      return SQueryResultConverter.convert(resp.getBody(), AProductChangeConverter.getInstance());
    else
      throw new ThorServiceException("无法查询到商品变更单：" + resp.getMessage());
  }

  @Autowired
  private SProductChangeService sProductChangeService;

}
